package com.wlyuan.open.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.wlyuan.area.api.IBasicAreaService;
import com.wlyuan.area.dto.AnalyzerAreaDTO;
import com.wlyuan.basic.api.IShippingAddressService;
import com.wlyuan.basic.dto.customer.ShippingAddressFilterDTO;
import com.wlyuan.core.exception.AssertUtils;
import com.wlyuan.core.exception.ValueError;
import com.wlyuan.core.marker.NamedStatus;
import com.wlyuan.open.annotation.OpenApi;
import com.wlyuan.open.assembler.AddressAssembler;
import com.wlyuan.open.basic.dto.OpenShippingAddressCreationDTO;
import com.wlyuan.open.basic.dto.OpenShippingAddressUpdateDTO;
import com.wlyuan.open.basic.vo.OpenShippingAddressDetailVO;
import com.wlyuan.open.translator.AddressTranslator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.var;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(tags = "发货地址")
@Validated
@RequestMapping("/api/shippingAddress")
@RestController
public class OpenShippingAddressController {

    @DubboReference(check = false)
    private IShippingAddressService shippingAddressService;

    @DubboReference(check = false)
    private IBasicAreaService basicAreaService;

    @ApiOperation("创建发货地址")
    @PostMapping("/")
    @OpenApi
    public OpenShippingAddressDetailVO createReceivingAddress(@RequestBody @Valid OpenShippingAddressCreationDTO openShippingCreation) {
        //校验省市区及得到编码
        var area = basicAreaService.matchArea(new AnalyzerAreaDTO(openShippingCreation.getProvince(), openShippingCreation.getCity(), openShippingCreation.getDistrict()));
        var shippingAddress = AddressAssembler.assemble(openShippingCreation, area);
        var shippingDetail = shippingAddressService.create(shippingAddress);
        return AddressTranslator.translate(shippingDetail);
    }

    @ApiOperation("修改发货地址")
    @PutMapping("/")
    @OpenApi
    public OpenShippingAddressDetailVO updateShippingAddress(@RequestBody @Valid OpenShippingAddressUpdateDTO openUpdate) {
        //校验省市区及得到编码
        var area = basicAreaService.matchArea(new AnalyzerAreaDTO(openUpdate.getProvince(), openUpdate.getCity(), openUpdate.getDistrict()));
        var shippingAddress = AddressAssembler.assemble(openUpdate, area);
        return AddressTranslator.translate(shippingAddressService.update(shippingAddress));
    }

    //todo 有自己得实体类
    @ApiOperation("根据code获取最新发货地址")
    @PostMapping("/code/search")
    @OpenApi
    public OpenShippingAddressDetailVO getAddress(@RequestBody OpenShippingAddressCreationDTO code) {
        AssertUtils.assertNotEmpty(code.getAddressCode(), new ValueError("收货地址code不能为空"));
        ShippingAddressFilterDTO filterDTO = new ShippingAddressFilterDTO();
        filterDTO.setTenantId(code.getTenantId());
        filterDTO.setAddressCode(code.getAddressCode());
        filterDTO.setStatus(NamedStatus.ENABLED.getValue());
        filterDTO.setPage(1L);
        filterDTO.setSize(1L);
        var result = shippingAddressService.search(filterDTO);
        if (result != null && CollectionUtil.isNotEmpty(result.getRecords())) {
            return AddressTranslator.translate(result.getRecords().get(0));
        }
        return null;
    }

}
